﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

using Frame.WcfContract;

namespace Frame.WcfClient
{
    public class WcfClient : ClientBase<IWcfService>
    {
        public WcfClient()
            : base()
        {

        }

        public WcfClient(string endpointConfigurationName)
            : base(endpointConfigurationName)
        {

        }

        public WcfClient(Binding binding, EndpointAddress address)
            : base(binding, address)
        {

        }
        private void ChannelSet()
        {
            this.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 0);
        }
        bool useByte = false;
        public bool ExecCommand(ref Task task, ref string errMsg)
        {
            bool result = true;

            ChannelSet();

            Task taskres = null;
            if (useByte)
            {
                byte[] buffer = this.ExecCommandByte(task, ref result, ref errMsg);
                taskres = Frame.Common.ZipSerializable.DeserializeObject(buffer) as Task;
            }
            else
            {
                taskres = this.ExecCommandTask(task, ref result, ref errMsg);
            }

            task = taskres;
            return result;
        }

        private byte[] ExecCommandByte(Task task, ref bool result, ref string errMsg)
        {
            return this.Channel.ExecCommandByte(task, ref result, ref errMsg);
        }

        private Task ExecCommandTask(Task task, ref bool result, ref string errMsg)
        {
            return this.Channel.ExecCommandTask(task, ref result, ref errMsg);
        }
    }
}
